/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package mf.demo.client;

import java.io.IOException;
import java.io.InputStream;
import mf.demo.BaseUtil;
import mf.demo.IService;
import mf.demo.User;
import mf.mfrpc.serialize.DataSource;
import mf.mfrpc.serialize.DataSourceIterator;
import mf.mfrpc.serialize.FileDataSource;
import mf.mfrpc.serialize.FileDataSourceIterator;
import org.apache.commons.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * WSClient
 */
public class WSClient {

    private static final String PREFIX = "http://localhost:8080/MFrpc/ws";

    private static final String testURL = PREFIX + "/test.ws";

    //WebService
    static IService is = BaseUtil.getWSHttpClient(testURL).create(IService.class);

    //log
    private static final Logger LOG = LoggerFactory.getLogger(RPCClient.class);

    //
    public static void testObject() {
        Object res = is.testObject();
        LOG.info("res type : [{}]", res.getClass());
        LOG.info("res : [{}]", res);
    }

    //
    public static void testBean() {
        User u = is.testBean("ad", "098765");
        LOG.info("res : [{}]", u);
    }

    //
    public static void testInputStream() throws IOException {
        InputStream in = is.testInputStream(BaseUtil.getTestFile1().getPath());
        LOG.info(Streams.asString(in, "utf-8"));
    }

    //
    public static void testDataSource() throws IOException {
        DataSource ds = is.testDataSource(BaseUtil.getTestFile2().getPath());
        LOG.info("{},{},{}", new Object[]{ds.getName(), ds.getSize(), ds.getLastModified()});
        LOG.info(Streams.asString(ds.getInputStream(), "utf-8"));
    }

    //
    public static void testUploadDataSource() throws IOException {
        FileDataSource fds0 = new FileDataSource(BaseUtil.getTestFile1());
        is.testUploadDataSource(fds0);
    }

    //
    public static void testUploadDataSources() throws IOException {
        FileDataSource fds1 = new FileDataSource(BaseUtil.getTestFile1());
        FileDataSource fds2 = new FileDataSource(BaseUtil.getTestFile2());
        DataSourceIterator di = new FileDataSourceIterator(fds1, fds2);
        LOG.info("res : [{}]", is.testUploadDataSources(di));
    }

    public static void main(String[] args) throws IOException {
        testObject();
        System.out.println("----------------------------------------");
        testBean();
        System.out.println("----------------------------------------");
        testInputStream();
        System.out.println("----------------------------------------");
        testDataSource();
        System.out.println("----------------------------------------");
        testUploadDataSource();
        System.out.println("----------------------------------------");
        testUploadDataSources();
        System.out.println("----------------------------------------");
    }
}
